package gold.digger;

import gold.vo.TreeNode;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC257 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> res = new ArrayList<>();
            StringBuilder curPath = new StringBuilder();
            binaryTreePathsDFS(root, curPath, res);
            return res;
        }

        public void binaryTreePathsDFS(TreeNode root, StringBuilder curPath, List<String> res) {
            if (null == root) return;
            if (null == root.left && null == root.right) {
                int curLength = curPath.length();
                curPath.append("->").append(root.val);
                res.add(curPath.toString().substring(2));
                curPath.delete(curLength, curPath.length());
                return;
            }

            int curLength = curPath.length();
            curPath.append("->").append(root.val);
            binaryTreePathsDFS(root.left, curPath, res);
            binaryTreePathsDFS(root.right, curPath, res);
            curPath.delete(curLength, curPath.length());
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode ro = TreeNode.createTree("1,2,3");
        System.out.println(solution.binaryTreePaths(ro));
    }

    public static void main(String[] args) throws Exception {
        LC257 an = new LC257();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
